package com.zjy.controller;

import com.zjy.api.Api;
import com.zjy.bean.Article;
import com.zjy.bean.UserCollection;
import com.zjy.dto.*;
import com.zjy.service.ArticleService;
import com.zjy.service.GoodsService;
import com.zjy.utils.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

import static com.zjy.api.Api.CLIENT_ERROR;
import static com.zjy.api.Api.SERVER_ERROR;

/**
 * Created by Administrator on 2019/3/14.
 */
@Controller
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @Resource
    private GoodsService goodsService;
    /**
     * 新增博文
     * @param articleParam
     * @return
     */
    @RequestMapping(value = "/addArticle",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addArticle(@RequestBody ArticleParam articleParam){
        ApiResult result = new ApiResult();
        Article article = articleParam.getArticle();
        if(ObjectUtils.isNullOrEmpty(article)){
            result.error(CLIENT_ERROR,"文章不能为空！");
            return result;
        }else {
            try {
                articleService.addArticle(articleParam);
                result.setMsg("文章插入成功！");
            }catch (Exception e){
                result.error(Api.SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 通过文章Id删除文章(软删除)
     * @param article
     * @return
     */
    @RequestMapping(value = "/deleteArticleById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteArticleById(@RequestBody Article article){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(article) || ObjectUtils.isNullOrEmpty(article.getId())){
            result.error(CLIENT_ERROR,"缺少参数：id");
            return result;
        }else {
            try{
                String articleId = article.getId();
                int count = articleService.deleteArticleById(articleId);
                if(count == 0){
                    result.error(CLIENT_ERROR,"文章id有误！");
                    return result;
                }else if(count == 1){
                    result.setMsg("删除成功！");
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     *通过Id更新文章的非空字段
     * @param articleParam
     * @return
     */
    @RequestMapping(value = "/updateArticleById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateArticleById(@RequestBody ArticleParam articleParam){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(articleParam) || ObjectUtils.isNullOrEmpty(articleParam.getArticle().getId())){
            result.error(CLIENT_ERROR,"缺少参数:id");
            return result;
        }else {
            try{
                int count = articleService.updateArticleById(articleParam);
                if(count == 0){
                    result.error(SERVER_ERROR,"更新失败！");
                    return result;
                }else if(count == 1){
                    result.setMsg("更新成功！");
                    return result;
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

    /**
     * 通过articleId、userId查找文章
     * @param article
     * @return
     */
    @RequestMapping(value = "/selectArticleById",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectArticleById(@RequestBody Article article){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(article.getUserId()) || ObjectUtils.isNullOrEmpty(article.getId())){
            result.error(CLIENT_ERROR,"userId和id不能为空");
            return result;
        }else{
            try{
                String articleId = article.getId();
                ArticleResponse articleResponse = articleService.selectArticleById(article.getUserId(),articleId);
                if (ObjectUtils.isNullOrEmpty(articleResponse.getArticle())) {
                    result.setMsg("找不到此id对应的文章！");
                }else {
                    result.setMsg("查找文章成功！");
                    result.setData(articleResponse);
                }
            }catch (Exception e){
                result.error(SERVER_ERROR,e.getMessage());
                return result;
            }
        }
        return result;
    }

//    /**
//     * 查找所有文章
//     * @param
//     * @return
//     */
//    @RequestMapping(value = "/selectAllArticle",method = RequestMethod.POST)
//    @ResponseBody
//    public ApiResult selectAllArticle(){
//        ApiResult result = new ApiResult();
//        try{
//            List<ArticleResponse> articleResponseList = articleService.selectAllArticle();
//            if(!ObjectUtils.isNullOrEmpty(articleResponseList)){
//                result.setMsg("查找文章列表成功！");
//            }
//            result.setData(articleResponseList);
//        }catch (Exception e){
//            return result.error(SERVER_ERROR,e.getMessage());
//        }
//        return result;
//    }

    /**
     * 查找符合条件的文章
     * @param articleCondition
     * @return
     */
    @RequestMapping(value = "/selectArticleByCondition",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult selectArticleByCondition(@RequestBody ArticleCondition articleCondition){
        ApiResult result = new ApiResult();
        try{
            ListResponse response = articleService.selectArticleByCondition(articleCondition);
            if(ObjectUtils.isNullOrEmpty(response)){
                result.setMsg("找不到对应的文章列表！");
            }else {
                result.setMsg("查找文章列表成功！");
                result.setData(response);
            }
        }catch (Exception e){
            result.error(SERVER_ERROR,e.getMessage());
            return result;
        }
        return result;
    }

    /**
     * 点赞博文
     * @param article
     * @return
     */
    @RequestMapping(value = "/addLike",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addLike(@RequestBody Article article){
        ApiResult result = new ApiResult();
        String userId = article.getUserId();  //点赞者的Id
        String articleId = article.getId(); //被点赞的文章Id
        if(ObjectUtils.isNullOrEmpty(userId)|| ObjectUtils.isNullOrEmpty(articleId)){
            return result.error(CLIENT_ERROR,"缺少参数:userId或articleId");
        }else {
            try{
                articleService.addLike(userId,articleId);
                result.setMsg("点赞成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }
        return result;
    }

    /**
     * 取消点赞博文
     * @param article
     * @return
     */
    @RequestMapping(value = "/cancelLike",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult cancelLike(@RequestBody Article article){
        ApiResult result = new ApiResult();
        String userId = article.getUserId();  //点赞者的Id
        String articleId = article.getId(); //被点赞的文章Id
        if(ObjectUtils.isNullOrEmpty(userId)|| ObjectUtils.isNullOrEmpty(articleId)){
            return result.error(CLIENT_ERROR,"缺少参数:userId或articleId");
        }else {
            try{
                articleService.cancelLike(userId,articleId);
                result.setMsg("取消点赞成功！");
            }catch (Exception e){
                return result.error(SERVER_ERROR,e.getMessage());
            }
        }
        return result;
    }

    /**
     * 收藏博文
     * @param userCollection
     * @return
     */
    @RequestMapping(value = "/addArticleCollection",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addArticleCollection(@RequestBody UserCollection userCollection){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(userCollection.getTargetId()) || ObjectUtils.isNullOrEmpty(userCollection.getUserId())){
            return result.error(CLIENT_ERROR,"targetId和userId不能为空！");
        }
        try{
            int count = articleService.addArticleCollection(userCollection);
            if(count == 1){
                result.setMsg("收藏文章成功！");
            }else if(count == 0){
                result.error(CLIENT_ERROR,"收藏文章失败！没有找到此targetId对应的文章");
            }

        }catch (Exception e){
            return result.error(SERVER_ERROR,"收藏文章失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 取消收藏文章
     * @return
     */
    @RequestMapping(value = "/cancelArticleCollection",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult cancelArticleCollection(@RequestBody UserCollection userCollection){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(userCollection.getTargetId()) || ObjectUtils.isNullOrEmpty(userCollection.getUserId())){
            return result.error(CLIENT_ERROR,"targetId和userId不能为空！");
        }
        try{
            int count = articleService.cancelArticleCollection(userCollection);
            if(count == 1){
                result.setMsg("取消收藏商品成功！");
            }else if(count == 0){
                result.error(CLIENT_ERROR,"取消收藏商品失败！没有找到此targetId对应的商品");
            }

        }catch (Exception e){
            return result.error(SERVER_ERROR,"取消收藏商品失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 批量删除用户文章收藏
     * @return
     */
    @RequestMapping(value = "/deleteArticleCollection",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteArticleCollection(@RequestBody HashMap<String,Object> param){
        ApiResult result = new ApiResult();
        if(ObjectUtils.isNullOrEmpty(param.get("userId"))){
            return result.error(CLIENT_ERROR,"userId不能为空！");
        }
        try{
            int count = goodsService.deleteGoodsCollection(param);
            if(count == 0){
                result.setMsg("找不到该收藏关系！");
            }else if(count > 0){
                result.setMsg("批量删除文章收藏成功！");
            }
        }catch (Exception e){
            return result.error(SERVER_ERROR,"批量删除文章收藏失败，原因："+e.getMessage());
        }
        return result;
    }

    /**
     * 查询文章总数、今日新增文章数
     * @return
     */
    @RequestMapping(value = "/getArticleNum", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getArticleNum(){
        ApiResult result = new ApiResult();
        try{
            HashMap<String,Object> map = articleService.getArticleNum();
            if(!ObjectUtils.isNullOrEmpty(map)){
                result.setMsg("数据查询成功！");
                result.setData(map);
            }else {
                result.setMsg("数据查询失败");
            }
        }catch (Exception e){
            return result.error(SERVER_ERROR,"数据查询失败，原因："+ e.getMessage());
        }
        return result;
    }
}
